<template>
	<view class="uni-product-list">
		<view class="uni-product"  v-if="productList.length!=0" v-for="(product,index) in productList" :key="index" @click="toProductDetail(product.id)">
			<view class="image-view">
				<image v-if="renderImage" class="uni-product-image" :src="product.defaultImg"></image>
			</view>

			<view class="uni-product-title">{{product.name}}</view>
			<view class="uni-product-price">
				<text class="uni-product-price-favour">￥{{product.favourPrice}}</text>
				<text class="uni-product-price-original">￥{{product.price}}/天</text>
				<text class="uni-product-tip">自营</text>
			</view>

		</view>
		<van-empty style="margin-left: 123px;" v-if="productList.length==0" description="暂无此商品" />


	</view>
</template>

<script>
	export default {
		data() {
			return {
				categoriesId: 0,
				title: 'product-list',
				productList: [],
				renderImage: false
			};
		},
		methods: {
			toProductDetail(id){
				uni.navigateTo({
					url: 'product-detail?productId='+id
				})
			},
			loadData(action = 'add') {
				this.$request('/product/list?categories=' + this.categoriesId, {
					// 传参参数名：参数值,如果没有，就不需要传
				}, 'GET').then(res => {
					this.productList = res.records;
				}).catch(err => {
					console.log(err)
				})

				if (action === 'refresh') {
					this.productList = [];
				}

				// data.forEach(item => {
				//     this.productList.push(item);
				// });
			}
		},
		onLoad(e) {
			this.categoriesId = e.productId
			console.log(e.productId);
			this.loadData();
			setTimeout(() => {
				this.renderImage = true;
			}, 300);
		},
		onPullDownRefresh() {
			this.loadData('refresh');
			// 实际开发中通常是网络请求，加载完数据后就停止。这里仅做演示，加延迟为了体现出效果。
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 2000);
		},
		onReachBottom() {
			this.loadData();
		}
	};
</script>

<style>
	/* product */
	page {
		background: #F8F8F8;
	}

	view {
		font-size: 28upx;
	}

	.uni-product-list {
		display: flex;
		width: 100%;
		flex-wrap: wrap;
		flex-direction: row;
	}

	.uni-product {
		padding: 20upx;
		display: flex;
		flex-direction: column;
	}

	.image-view {
		height: 330upx;
		width: 330upx;
		margin: 12upx 0;
	}

	.uni-product-image {
		height: 330upx;
		width: 330upx;
	}

	.uni-product-title {
		width: 300upx;
		word-break: break-all;
		display: -webkit-box;
		overflow: hidden;
		line-height: 1.5;
		text-overflow: ellipsis;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.uni-product-price {
		margin-top: 10upx;
		font-size: 28upx;
		line-height: 1.5;
		position: relative;
	}

	.uni-product-price-original {
		color: #e80080;
	}

	.uni-product-price-favour {
		color: #888888;
		text-decoration: line-through;
		margin-left: 10upx;
	}

	.uni-product-tip {
		position: absolute;
		right: 10upx;
		background-color: #ff3333;
		color: #ffffff;
		padding: 0 10upx;
		border-radius: 5upx;
	}
</style>
